package com.ruoyi.web.controller.webservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.asset.common.AssetStatusType;
import com.ruoyi.asset.common.AssetTransactionRecordType;
import com.ruoyi.asset.domain.AssetTransactionRecord;
import com.ruoyi.asset.domain.DisposalRecord;
import com.ruoyi.asset.domain.FixedAssets;
import com.ruoyi.asset.mapper.AssetTransactionRecordMapper;
import com.ruoyi.asset.mapper.DisposalRecordMapper;
import com.ruoyi.asset.mapper.FixedAssetsMapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.web.controller.webservice.FixedAssetsWebService;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author jiangxiangjin
 * @version V1.0.0
 * @description
 * @date 2023/4/22 14:46
 */
@Component
@Slf4j
public class FixedAssetsWebServiceImpl extends BaseController implements FixedAssetsWebService {

    @Autowired
    private FixedAssetsMapper fixedAssetsMapper;

    @Autowired
    private AssetTransactionRecordMapper assetTransactionRecordMapper;

    @Autowired
    private DisposalRecordMapper disposalRecordMapper;


    @Autowired
    private DictService dictService;


    @Override
    public String addAsset(String user) {

        log.info(user);

        return "固定资产管理系统";
    }

    /**
     * 通过id查询用户有那些设备
     *
     * @param userId
     * @return
     */
    @Override
    public String selectFixedAssetByUserId(String userId) {
        LambdaQueryWrapper<FixedAssets> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(userId), FixedAssets::getUserId, userId);


        List<FixedAssets> fixedAssets = fixedAssetsMapper.selectList(lambdaQueryWrapper);

        fixedAssets.forEach(fixedAsset -> {
            fixedAsset.setAssetStatus(dictService.getLabel("asset_status", fixedAsset.getAssetStatus()));
            fixedAsset.setAssetType(dictService.getLabel("asset_type", fixedAsset.getAssetType()));
            fixedAsset.setAssetBrand(dictService.getLabel("asset_brand", fixedAsset.getAssetBrand()));
            fixedAsset.setAssetCategory(dictService.getLabel("asset_category", fixedAsset.getAssetCategory()));
            fixedAsset.setAssetAttribute(dictService.getLabel("asset_attribut", fixedAsset.getAssetAttribute()));
            fixedAsset.setSupplier(dictService.getLabel("asset_supplier", fixedAsset.getSupplier()));
        });


        return JSON.toJSONString(fixedAssets);
    }

    /**
     * 固定资产调用接口
     *
     * @param var 接受的json参数
     * @return
     */
    @Transactional
    public String invokeFixedAsset(String var) {
        JSONObject jsonObject = JSON.parseObject(var);
        String assetNumber = (String) jsonObject.get("assetNumber");
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        if (fixedAssets != null) {
            String chuZhi = (String) jsonObject.get("处置状态");
            String borrower = (String) jsonObject.get("borrower");
            String borrowerName = (String) jsonObject.get("borrowerName");
            String department = (String) jsonObject.get("department");
            String returner = (String) jsonObject.get("returner");
            String returnerName = (String) jsonObject.get("returnerName");
            Date transactionDate = (Date) jsonObject.get("transactionDate");
            /**
             * TODO
             * 如果web接口传参不对
             * 就把这个归还日期去掉，不从前端获取，设置为流程当日
             */
            Date returnDate = (Date)jsonObject.get("returnDate");
            AssetTransactionRecord transactionRecord = assetTransactionRecordMapper.selectById(assetNumber);
            String transactionType = transactionRecord.getTransactionType();
            if (chuZhi == "调用") {
                if (transactionRecord.equals(AssetTransactionRecordType.CLAIM) || transactionRecord.equals(AssetTransactionRecordType.BORROW)) {
                    transactionRecord.setTransactionType(AssetTransactionRecordType.SLUGGISH);
                    transactionRecord.setReturnDate(returnDate);
                    transactionRecord.setReturner(returner);
                    transactionRecord.setReturnerName(returnerName);
                    int i = assetTransactionRecordMapper.updateById(transactionRecord);
                    if (i == 1) {
                        AssetTransactionRecord assetTransactionRecord = null;
                        assetTransactionRecord.setBorrower(borrower);
                        assetTransactionRecord.setBorrowerName(borrowerName);
                        assetTransactionRecord.setTransactionDate(transactionDate);
                        fixedAssets.setUserId(borrower);
                        fixedAssets.setUserName(borrowerName);
                        fixedAssets.setDepartment(department);
                        fixedAssetsMapper.insert(fixedAssets);
                        return JSON.toJSONString(toAjax(assetTransactionRecordMapper.insert(assetTransactionRecord)));
                    }
                }
            }
            if (chuZhi == "领用" || chuZhi == "借用") {
                if (transactionRecord.equals(AssetTransactionRecordType.CLAIM) || transactionRecord.equals(AssetTransactionRecordType.BORROW)) {

                }
            }
            String assetStatus = fixedAssets.getAssetStatus();
            if (assetStatus.equals(AssetStatusType.NORMAL) ) {
                fixedAssets.setDepartment(department);
                fixedAssets.setUserName(borrowerName);
                fixedAssets.setUserName(borrowerName);
                String s = JSON.toJSONString(toAjax(fixedAssetsMapper.updateById(fixedAssets)));
                return s;
            }
        }
        return JSON.toJSONString(toAjax(false));


    }

    /**
     * 固定资产归还接口
     *
     * @param var
     * @return
     */
    public String returnFixedAsset(String var) {
        JSONObject jsonObject = JSON.parseObject(var);
        String userId = (String) jsonObject.get("userId");
        String userName = (String) jsonObject.get("userName");
        String assetNumber = (String) jsonObject.get("assetNumber");
        String oaNumber = (String) jsonObject.get("oaNumber");

        LambdaQueryWrapper<AssetTransactionRecord> atryQueryWrapper = new LambdaQueryWrapper<>();
        atryQueryWrapper.eq(StringUtils.isNotEmpty(userId), AssetTransactionRecord::getBorrower, userId);
        atryQueryWrapper.eq(StringUtils.isNotEmpty(assetNumber), AssetTransactionRecord::getAssetNumber, assetNumber);
        atryQueryWrapper.ne(AssetTransactionRecord::getTransactionType, "2");


//        atryQueryWrapper.and(wrapper->{
//            wrapper.eq(AssetTransactionRecord::getTransactionType,"0").or().eq(AssetTransactionRecord::getTransactionType,"1");
//        });

        AssetTransactionRecord assetTransactionRecord = assetTransactionRecordMapper.selectOne(atryQueryWrapper);
        String result = null;
        if (assetTransactionRecord == null) {
            result = JSON.toJSONString(toAjax(assetTransactionRecord != null));
            return result;

        }

        assetTransactionRecord.setReturner(userId);
        assetTransactionRecord.setReturnerName(userName);
        assetTransactionRecord.setTransactionType("2");
        assetTransactionRecord.setReturnOaNumber(oaNumber);

        int i = assetTransactionRecordMapper.updateById(assetTransactionRecord);


        result = JSONObject.toJSONString(toAjax(i));

        return result;

    }


    /**
     * 固定资产销毁接口
     *
     * @param var 接受的json参数
     * @return
     */
    @Transactional
    public String disposalFixedAsset(String var) {
        JSONObject jsonObject = JSONObject.parseObject(var);
        String assetNumber = (String) jsonObject.get("assetNumber");
        String oaNumber = (String) jsonObject.get("oaNumber");


        LambdaQueryWrapper<FixedAssets> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FixedAssets::getAssetNumber, assetNumber);
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);

        fixedAssets.setAssetStatus("2");

        fixedAssetsMapper.updateById(fixedAssets);


        DisposalRecord disposalRecord = new DisposalRecord();
        disposalRecord.setDisposalId(oaNumber);
        disposalRecord.setAssetNumber(assetNumber);
        disposalRecord.setDisposalType("0");
        disposalRecord.setDisposalDate(new Date());

        disposalRecordMapper.insert(disposalRecord);


        return null;
    }

}
